package com.service;

import com.mapper.C_userDao;
import com.mapper.P_inforDao;
import com.mapper.P_userDao;
import com.util.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class P_userService {
    @Resource
    private P_userDao p_userDao;
    @Resource
    private P_inforDao p_inforDao;
    @Resource
    private P_register p_register;
    @Resource
    private C_userDao c_userDao;

    public Result<?> login(P_user p_user) {
        P_user p = p_userDao.selectByAccount(p_user);
        Result result=new Result();
        if (p == null) {
            Result r=result.error(result,"0","用户名或者密码错误");
            return r;
        } else {
            if (p_user.getpPassword().equals(p.getpPassword())) {
                Result r=result.success(p.getId(),result);
                return r;
            } else {
                Result r=result.error(result,"0","用户名或者密码错误");
                return r;
            }
        }
    }
    public Result<?> register(P_register p_register) {
        P_user p_user=new P_user();
        p_user.setpPassword(p_register.getpPassword());
        p_user.setpAccount(p_register.getpAccount());
        p_user.setId(p_register.getId());
        P_infor p_infor=new P_infor();
        p_infor.setpAccount(p_register.getpAccount());
        p_infor.setpName(p_register.getpName());
        p_infor.setEmail(p_register.getEmail());
        p_infor.setPhone(p_register.getPhone());
        p_infor.setCode(p_register.getCode());
        p_infor.setpId(p_register.getpId());
        p_infor.setTitle(p_register.getTitle());
        p_infor.setEnable(p_register.getEnable());
        P_user p=p_userDao.selectByAccount(p_user);
        C_user c_user=new C_user();
        c_user.setCAccount(p_infor.getCode());
        C_user c=c_userDao.selectByAccount(c_user);
        Result result=new Result();
        if (p!=null) {
            Result r=result.error(result,"-1","该用户已存在");
            return r;
        } else if (c==null){
            Result r=result.error(result,"-2","所属公司不存在");
            return r;
        }else {
            p_userDao.insert(p_user);
            p_inforDao.insert(p_infor);
            Result r=result.success(result);
            return r;
        }
    }
    public Result<?> updatePassword(P_user p_user){
        Result result=new Result();
        p_userDao.updatePassword(p_user);
        Result r=result.success(result);
        return r;
    }

}
